package com.inei.evaluacionemype.model.dao;

import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.inei.evaluacionemype.model.connection.UtilDB;
import com.inei.evaluacionemype.model.entity.Cap01;

public class Cap01Dao implements ICRUD<Cap01> {

	// variables
	private UtilDB util;
	private SQLiteDatabase db;
	private Cursor cursor = null;

	// constructor
	public Cap01Dao(Context context) {
		util = new UtilDB(context);
	}

	// metodos CRUD
	@Override
	public Cap01 buscar(Cap01 o) {
		Cap01 obj = null;
		db = util.getReadableDatabase();

		cursor = db.rawQuery("select * from Cap01 where RUC=?",
				new String[] { o.getRUC() + "" });
		if (cursor.moveToFirst()) {
			obj = new Cap01();
			obj.setRUC(cursor.getString(cursor.getColumnIndex("RUC")));
			obj.setC1P2RASO(cursor.getString(cursor.getColumnIndex("C1P2RASO")));
			obj.setC1P3NOMCOMER(cursor.getString(cursor
					.getColumnIndex("C1P3NOMCOMER")));
			obj.setC1P4NUMTELEF1(cursor.getString(cursor
					.getColumnIndex("C1P4NUMTELEF1")));
			obj.setC1P5NUMTELEF2(cursor.getString(cursor
					.getColumnIndex("C1P5NUMTELEF2")));
			obj.setC1P6FAX(cursor.getString(cursor.getColumnIndex("C1P6FAX")));
			obj.setC1P7TIPOVIA(cursor.getString(cursor
					.getColumnIndex("C1P7TIPOVIA")));
			obj.setC1P8DOMICILIO(cursor.getString(cursor
					.getColumnIndex("C1P8DOMICILIO")));
			obj.setC1P9NRO(cursor.getString(cursor.getColumnIndex("C1P9NRO")));
			obj.setC1P10INT(cursor.getString(cursor.getColumnIndex("C1P10INT")));
			obj.setC1P11PISO(cursor.getString(cursor
					.getColumnIndex("C1P11PISO")));
			obj.setC1P12MZN(cursor.getString(cursor.getColumnIndex("C1P12MZN")));
			obj.setC1P13LOTE(cursor.getString(cursor
					.getColumnIndex("C1P13LOTE")));
			obj.setC1P14KM(cursor.getDouble(cursor.getColumnIndex("C1P14KM")));
			obj.setC1P15REFERENCIA(cursor.getString(cursor
					.getColumnIndex("C1P15REFERENCIA")));
			obj.setC1P16ANIO_INI(cursor.getString(cursor
					.getColumnIndex("C1P16ANIO_INI")));
			obj.setC1P17EMAIL(cursor.getString(cursor
					.getColumnIndex("C1P17EMAIL")));
			obj.setC1P18WEB(cursor.getString(cursor.getColumnIndex("C1P18WEB")));
			obj.setC1P19A_NOMCCDD(cursor.getString(cursor
					.getColumnIndex("C1P19A_NOMCCDD")));
			obj.setC1P19B_NOMCCPP(cursor.getString(cursor
					.getColumnIndex("C1P19B_NOMCCPP")));
			obj.setC1P19C_NOMCCDI(cursor.getString(cursor
					.getColumnIndex("C1P19C_NOMCCDI")));
			obj.setC1P20NOMCIIU(cursor.getString(cursor
					.getColumnIndex("C1P20NOMCIIU")));
			obj.setC1P21CLANAE(cursor.getString(cursor
					.getColumnIndex("C1P21CLANAE")));
			obj.setC1P22TIPOORG(cursor.getString(cursor
					.getColumnIndex("C1P22TIPOORG")));
			obj.setC1P23A_TELEFOFIJO(cursor.getString(cursor
					.getColumnIndex("C1P23A_TELEFOFIJO")));
			obj.setC1P23B_NOMBRES(cursor.getString(cursor
					.getColumnIndex("C1P23B_NOMBRES")));
			obj.setC1P23C_CARGO(cursor.getString(cursor
					.getColumnIndex("C1P23C_CARGO")));
			obj.setC1P23D_MOVIL(cursor.getString(cursor
					.getColumnIndex("C1P23D_MOVIL")));
			obj.setC1P23E_EMAIL(cursor.getString(cursor
					.getColumnIndex("C1P23E_EMAIL")));
		}

		Cerrar();
		return obj;
	}

	@Override
	public List<Cap01> leer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int crear(Cap01 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("RUC", o.getRUC());
		valores.put("C1P2RASO", o.getC1P2RASO());
		valores.put("C1P3NOMCOMER", o.getC1P3NOMCOMER());
		valores.put("C1P4NUMTELEF1", o.getC1P4NUMTELEF1());
		valores.put("C1P5NUMTELEF2", o.getC1P5NUMTELEF2());
		valores.put("C1P6FAX", o.getC1P6FAX());
		valores.put("C1P7TIPOVIA", o.getC1P7TIPOVIA());
		valores.put("C1P8DOMICILIO", o.getC1P8DOMICILIO());
		valores.put("C1P9NRO", o.getC1P9NRO());
		valores.put("C1P10INT", o.getC1P10INT());
		valores.put("C1P11PISO", o.getC1P11PISO());
		valores.put("C1P12MZN", o.getC1P12MZN());
		valores.put("C1P13LOTE", o.getC1P13LOTE());
		valores.put("C1P14KM", o.getC1P14KM());
		valores.put("C1P15REFERENCIA", o.getC1P15REFERENCIA());
		valores.put("C1P16ANIO_INI", o.getC1P16ANIO_INI());
		valores.put("C1P17EMAIL", o.getC1P17EMAIL());
		valores.put("C1P18WEB", o.getC1P18WEB());
		valores.put("C1P19A_NOMCCDD", o.getC1P19A_NOMCCDD());
		valores.put("C1P19B_NOMCCPP", o.getC1P19B_NOMCCPP());
		valores.put("C1P19C_NOMCCDI", o.getC1P19C_NOMCCDI());
		valores.put("C1P20NOMCIIU", o.getC1P20NOMCIIU());
		valores.put("C1P21CLANAE", o.getC1P21CLANAE());
		valores.put("C1P22TIPOORG", o.getC1P22TIPOORG());
		valores.put("C1P23A_TELEFOFIJO", o.getC1P23A_TELEFOFIJO());
		valores.put("C1P23B_NOMBRES", o.getC1P23B_NOMBRES());
		valores.put("C1P23C_CARGO", o.getC1P23C_CARGO());
		valores.put("C1P23D_MOVIL", o.getC1P23D_MOVIL());
		valores.put("C1P23E_EMAIL", o.getC1P23E_EMAIL());

		resultado = (int) db.insert("Cap01", null, valores);
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	@Override
	public int eliminar(Cap01 o) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int actualizar(Cap01 o) {
		int resultado = 0;
		db = util.getWritableDatabase();
		db.beginTransaction();

		ContentValues valores = new ContentValues();
		valores.put("C1P2RASO", o.getC1P2RASO());
		valores.put("C1P3NOMCOMER", o.getC1P3NOMCOMER());
		valores.put("C1P4NUMTELEF1", o.getC1P4NUMTELEF1());
		valores.put("C1P5NUMTELEF2", o.getC1P5NUMTELEF2());
		valores.put("C1P6FAX", o.getC1P6FAX());
		valores.put("C1P7TIPOVIA", o.getC1P7TIPOVIA());
		valores.put("C1P8DOMICILIO", o.getC1P8DOMICILIO());
		valores.put("C1P9NRO", o.getC1P9NRO());
		valores.put("C1P10INT", o.getC1P10INT());
		valores.put("C1P11PISO", o.getC1P11PISO());
		valores.put("C1P12MZN", o.getC1P12MZN());
		valores.put("C1P13LOTE", o.getC1P13LOTE());
		valores.put("C1P14KM", o.getC1P14KM());
		valores.put("C1P15REFERENCIA", o.getC1P15REFERENCIA());
		valores.put("C1P16ANIO_INI", o.getC1P16ANIO_INI());
		valores.put("C1P17EMAIL", o.getC1P17EMAIL());
		valores.put("C1P18WEB", o.getC1P18WEB());
		valores.put("C1P19A_NOMCCDD", o.getC1P19A_NOMCCDD());
		valores.put("C1P19B_NOMCCPP", o.getC1P19B_NOMCCPP());
		valores.put("C1P19C_NOMCCDI", o.getC1P19C_NOMCCDI());
		valores.put("C1P20NOMCIIU", o.getC1P20NOMCIIU());
		valores.put("C1P21CLANAE", o.getC1P21CLANAE());
		valores.put("C1P22TIPOORG", o.getC1P22TIPOORG());
		valores.put("C1P23A_TELEFOFIJO", o.getC1P23A_TELEFOFIJO());
		valores.put("C1P23B_NOMBRES", o.getC1P23B_NOMBRES());
		valores.put("C1P23C_CARGO", o.getC1P23C_CARGO());
		valores.put("C1P23D_MOVIL", o.getC1P23D_MOVIL());
		valores.put("C1P23E_EMAIL", o.getC1P23E_EMAIL());

		resultado = (int) db.update("Cap01", valores, "RUC=?",
				new String[] { o.getRUC() + "" });
		if (resultado > 0) {
			db.setTransactionSuccessful();
		}

		valores.clear();
		Cerrar();
		return resultado;
	}

	private void Cerrar() {
		if (cursor != null) {
			cursor.close();
		}
		if (db.inTransaction()) {
			db.endTransaction();
		}
		if (db.isOpen()) {
			db.close();
		}
	}
}
